<?php

require_once 'includes/constants.php';
require_once 'includes/class-cart.php';

$pid = $_POST['pid'];
$action = $_POST['action'];
if (!empty($pid) && $action == 'add') {
    Buffalo_Cart::add_to_cart($pid);
}

$term = $_GET[CATEGORY_TAXONOMY];

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => CATEGORY_TAXONOMY,
            'field' => 'slug',
            'terms' => $term
        )
    )
);

global $wp_query;
$wp_query = new WP_Query($args);

while ($wp_query->have_posts()) {
    $wp_query->the_post();

    echo '<h2><a href="' . get_permalink() . '">';
    the_title();
    echo '</a></h2>';

    $price = get_post_meta(get_the_ID(), PRICE_META_KEY, true);
    echo '<p>' . $price . '</p>';

    echo '<form method="post">';
    echo '<input name="pid" type="hidden" value="' . get_the_ID() . '" />';
    echo '<input name="action" type="hidden" value="add" />';
    echo '<input type="submit" value="Add to Cart" />';
    echo '</form>';
}
?>
